//js获取项目根路径，如： http://localhost:8083/uimcardprj
function getRootPath(){
    //获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
    var curWwwPath=window.document.location.href;
    //获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
    var pathName=window.document.location.pathname;
    var pos=curWwwPath.indexOf(pathName);
    //获取主机地址，如： http://localhost:8083
    var localhostPaht=curWwwPath.substring(0,pos);
    //获取带"/"的项目名，如：/uimcardprj
    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
    return(localhostPaht + projectName);
}
var contextPath = getRootPath();    //应用跟路径


/**
 * 提交试卷
 */
function submitExam(){
    //加载数据
    let userAnswerArr = [];
    // let i = 0;
    $("#doExamForm div textarea").toArray().forEach(item => {
        let $item = $(item);
        let userAnswer = {
            answer: $item.val(),
            question:{
                id: $item.attr("question_id")
            },
            order:  $item.attr("order")
        };
        userAnswerArr.push(userAnswer);
    });
    console.log(userAnswerArr);
    let data = {
        testPaper: {
            id: $("#testpaper_id").val()
        },
        answerList: userAnswerArr
    }
    console.log(data);
    let url = contextPath + "/exam/submitExam";
    axios.post(url, data).then(
    response => {
        console.log(response.data); //保存结果
        //保存成功，跳转到列表页面
        window.location.href = contextPath + "/exam/examPaperList";
    }).catch(response => {
        alert('请求失败');
    });

}

$(() => {
    $("#submitBtn").on("click", submitExam);    //提交按钮绑定事件


});